NSPopUpButton是一个下拉按钮,当用户点击时,其会弹出一个下拉选择菜单。一个简单的示例如下:
1 2 3 4 5 6 7 8 9 10 11 12
| - (void)viewDidLoad { [super viewDidLoad]; NSPopUpButton * popUpButton = [[NSPopUpButton alloc]initWithFrame:CGRectMake(100, 400, 200, 300)]; NSMenu * menu = [[NSMenu alloc]initWithTitle:@"menu"]; [menu insertItemWithTitle:@"one" action:@selector(null) keyEquivalent:@"" atIndex:0]; [menu addItemWithTitle:@"two" action:@selector(null) keyEquivalent:@""]; popUpButton.menu = menu; popUpButton.preferredEdge = NSRectEdgeMaxX; [self.view addSubview:popUpButton]; }
|
效果如下图所示:
NSPopUpButton继承与NSButton,因此NSButton添加触发事件的方式在NSPopUpButton中依然使用,NSPopUpButton类中属性和方法解析如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| - (instancetype)initWithFrame:(NSRect)buttonFrame pullsDown:(BOOL)flag;
@property (nullable, strong) NSMenu *menu;
@property BOOL autoenablesItems;
@property BOOL pullsDown;
@property NSRectEdge preferredEdge;
- (void)addItemsWithTitles:(NSArray<NSString *> *)itemTitles;
- (void)insertItemWithTitle:(NSString *)title atIndex:(NSInteger)index;
- (void)removeItemWithTitle:(NSString *)title;
- (void)removeItemAtIndex:(NSInteger)index;
- (void)removeAllItems;
@property (readonly, copy) NSArray<NSMenuItem *> *itemArray;
@property (readonly) NSInteger numberOfItems;
- (NSInteger)indexOfItem:(NSMenuItem *)item; - (NSInteger)indexOfItemWithTitle:(NSString *)title; - (NSInteger)indexOfItemWithTag:(NSInteger)tag; - (NSInteger)indexOfItemWithRepresentedObject:(nullable id)obj; - (NSInteger)indexOfItemWithTarget:(nullable id)target andAction:(nullable SEL)actionSelector;
- (nullable NSMenuItem *)itemAtIndex:(NSInteger)index; - (nullable NSMenuItem *)itemWithTitle:(NSString *)title;
@property (nullable, readonly, strong) NSMenuItem *lastItem;
- (void)selectItem:(nullable NSMenuItem *)item; - (void)selectItemAtIndex:(NSInteger)index; - (void)selectItemWithTitle:(NSString *)title; - (BOOL)selectItemWithTag:(NSInteger)tag; - (void)setTitle:(NSString *)string;
@property (nullable, readonly, strong) NSMenuItem *selectedItem;
@property (readonly) NSInteger indexOfSelectedItem;
@property (readonly) NSInteger selectedTag;
- (void)synchronizeTitleAndSelectedItem;
- (NSString *)itemTitleAtIndex:(NSInteger)index;
@property (readonly, copy) NSArray<NSString *> *itemTitles;
@property (nullable, readonly, copy) NSString *titleOfSelectedItem;
APPKIT_EXTERN NSNotificationName NSPopUpButtonWillPopUpNotification;
|